<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="4" :sm="6">
            <a-form-item label="名称">
              <a-input placeholder="请输入名称" v-model="queryParam.advanceName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="6">
            <a-form-item label="类型">
              <!-- <j-dict-select-tag
                style="width: 100%"
                v-model="queryParam.advanceType"
                placeholder="请选择"
                dict-code="inconplantype"
              /> -->
              <a-select allowClear placeholder="请选择类型" v-model="queryParam.advanceType">
                <a-select-option :value="item.value" v-for="item in typeList" :key="item.id">{{
                  item.label
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="4" :sm="6">
            <a-form-item label="计划名称">
              <a-input placeholder="请输入" v-model="queryParam.advanceName"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :md="4" :sm="6">
            <a-form-item label="年份">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                :allowClear="false"
                value-format="yyyy-MM-dd"
                v-model="queryParam.advanceYear"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="6">
            <a-form-item label="申报单位">
              <!-- <a-input placeholder="请输入申报单位" v-model="queryParam.submitDeptName"></a-input> -->
              <j-select-depart
                v-model="queryParam.submitDeptId"
                :trigger-change="true"
                customReturnField="id"
                :multi="true"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="6">
            <a-form-item label="申报级别">
              <j-dict-select-tag
                style="width: 100%"
                placeholder="请选择"
                v-model="queryParam.submitLevel"
                dict-code="xj_submit_level"
              />
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      @change="handleTableChange"
    >
      <span slot="附件" slot-scope="text, record">
        <a-button size="small" @click="handleLookFile(record, '查看/上传附件')" type="link">
          <span v-if="record.buttonFlag == '0'">上传</span> 
          <span v-else>查看</span>
        </a-button>
      </span>
      <span slot="levelStatus" slot-scope="text">
        <j-ellipsis :value="text" :length="6" />
      </span>
      <span slot="actionName">
        <a-button size="small" @click="openCreatModel()" type="primary" icon="plus">新增</a-button>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="handleDetail(record, tiileCase[record.advanceType])">查看</a>
        <template v-if="record.buttonFlag == '0'">
          <a-divider type="vertical" />
          <a @click="handleEdit(record, tiileCase[record.advanceType])">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="handleDel(record)">
            <a>删除</a>
          </a-popconfirm>
        </template>
        <!-- <template v-if="record.buttonFlag == '0'">
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="handleDel(record)">
            <a>删除</a>
          </a-popconfirm>
        </template> -->
        <!-- <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item v-if="record.buttonFlag == '0'"> </a-menu-item>
            <a-menu-item> </a-menu-item>
          </a-menu>
        </a-dropdown> -->
      </span>
    </a-table>
    <!-- table区域-end -->

    <create ref="creatModel" @openModelView="handleAddSubmit"></create>
    <CheckAndEditModel @submitd="loadData()" ref="modalForm"></CheckAndEditModel>
    <FileModel ref="fileModel" @openModelView="handleFileSubmit"></FileModel>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import columns from './indexColumns'
import create from './create'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import moment from 'moment'
import CheckAndEditModel from '../CheckAndEditModel.vue'
import { axios } from '@/utils/request'
import FileModel from '../components/FileModel.vue'

export default {
  name: 'jh',
  mixins: [JeecgListMixin],
  components: {
    create,
    CheckAndEditModel,
    FileModel,
  },
  data() {
    return {
      // 表头
      columns: columns(this),
      tiileCase: {
        单位: '审计先进单位申报表',
        个人: '审计先进工作者申报表',
      },
      typeList: [
        {
          id: '1',
          label: '个人申报',
          value: '个人申报',
        },
        {
          id: '2',
          label: '单位申报',
          value: '单位申报',
        },
      ],
      role: '',
      url: {
        list: '/check/assessment/queryAdvanceByPage',
        add: '/check/assessment/addCheck',
        del: '/check/assessment/deleteAdvance',
        file: '/check/assessment/saveAdFileInfo',
      },
      yearPickShow: false,
      dataSource: [
        {
          序号: 1,
          名称: 'xxxxxxxxx',
          年份: '2021',
          类型: '年份',
          审批状态: '待审批',
          申报单位: 'xxxxxxx',
          申报人员: '张丽',
        },
      ],
    }
  },
  computed: {},
  mounted() {
    this.getRole()
  },
  methods: {
    async getRole() {
      this.role = await getAction(this.url.role)
    },
    openCreatModel() {
      this.$refs.creatModel.visible = true
    },
    // TODO 上传附件
    handleFileSubmit(params) {
      console.log(params, 'params')
      postAction(this.url.file, params).then((res) => {})
    },
    // TODO 上传附件
    handleLookFile(record, name) {
      // this.getFileList(record)
      // this.$refs.fileModel.visible = true
      this.$refs.fileModel.title = name
      this.$refs.fileModel.look(record)
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handlePanelChange(value) {
      this.queryParam.advanceYear = moment(value).format('YYYY')
      this.yearPickShow = false
      // this.$refs.form.validateField('advanceYear', (error) => {
      //   console.log(error)
      // })
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
      if(this.queryParam.advanceYear){
        this.queryParam.advanceYear = moment(this.queryParam.advanceYear.slice(0, 4)).format('YYYY')
      }
    },
    handleDel(record) {
      axios({
        url: this.url.del,
        method: 'delete',
        data: { id: record.id, advanceType: record.advanceType },
      }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.success(res.message)
        }
      })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>